/**
 * Class for work with vkontakte.ru. 
 */
function Facebook(){
  // initialization settings
  var APP_ID = 162859177101861; // id of application
  var LOCALE_CODE = "ru_RU";    // code that specified localization
  
  var LOGIN_REDIRECT_URL = "/process/facebook/";
  var LOGOUT_REDIRECT_URL = "/";


  /**
   * Initializes facebook API.
   */  
  this.initFacebookAPI = function()
  {
    window.fbAsyncInit = function()
    {
      FB.init({appId: APP_ID, status: true, cookie: true});
    };
    
    // parameters for facebook, change only if changed API
    var createScriptElement = function()
    {
      var el = document.createElement("script");
      el.type = "text/javascript";
      el.async = true;
      el.src = "http://connect.facebook.net/" + LOCALE_CODE + "/all.js";
      document.getElementById("fb-root").appendChild(el);
    };
    
    setTimeout(createScriptElement, 0);
  }
 
  /**
   * Authorizes user in facebook.com
   */
  this.doLogin = function()
  {
    FB.login(loginHandler)
  }
  
  /**
   * Processes login function request
   * 
   * @param {Object} response from facebook.com
   */
  var loginHandler = function(response)
  {
    if (response.session)
    {
      window.location = LOGIN_REDIRECT_URL;
    }
    else
    {
      js.include('rare_tools');
      UItools.showAuthError();
    }
  }
  
  /**
   * Logout user in vkontakte.ru
   */
  this.doLogout = function()
  {
    FB.logout(logoutHandler);
  }
  
  /**
   * Processes logout function request
   * 
   * @param {Object} response from facebook.com
   */
  var logoutHandler = function(response)
  {
    window.location = LOGOUT_REDIRECT_URL;
  }
}